/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uit.ieproject.utilites;

import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

/**
 *
 * @author Maivia
 */
public class TableUtil {

    public static long[] getIdsSelected(JTable table, int column) {
        long[] result;
        int[] selected = table.getSelectedRows();
        int numSelectedRegexs = selected.length;
        result = new long[numSelectedRegexs];
        if (numSelectedRegexs > 0) {
            for (int i = 0; i < numSelectedRegexs; i++) {
                result[i] = Long.valueOf(table.getValueAt(selected[i], column).toString());
            }
        }
        return result;
    }

    public static void formatTable(JTable table) {
        table.getTableHeader().setFont(new Font("Dialog", Font.BOLD, 12));
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(JLabel.CENTER);
        for (int i = 0; i < table.getColumnCount(); i++) {
            table.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
        }
    }

    public static long getLastSeq(DefaultTableModel model) {
        int numRows = model.getRowCount();
        long value = 0;
        if (numRows > 0) {
            value = Long.valueOf(model.getValueAt(numRows - 1, 0).toString());
        }
        return value;
    }

    public static void resetSeq(DefaultTableModel model) {
        int numRows = model.getRowCount();
        if (numRows > 0) {
            long previousSeq = Long.valueOf(model.getValueAt(0, 0).toString());
            if (previousSeq != 1L) {
                previousSeq = 1L;
                model.setValueAt((long) previousSeq, 0, 0);
            }
            for (int i = 1; i < numRows; i++) {
                long currentSeq = Long.valueOf(model.getValueAt(i, 0).toString());
                if (currentSeq - 1 > previousSeq) {
                    currentSeq = previousSeq + 1;
                    model.setValueAt((long) currentSeq, i, 0);
                }
                previousSeq = currentSeq;
            }
        }
    }
    
    public static void setTableMultiLine(JTable table, int numColumn) {
        table.setRowHeight(table.getRowHeight() * numColumn);
        TableColumnModel cmodel = table.getColumnModel();
        for (int i = 0; i < numColumn; i++) {
            cmodel.getColumn(i).setCellRenderer(new MultiLineCellRenderer());
        }
    }
}
